$(function() {
	/*
	var str = prompt("请输入内部密码：", "");
	if (str == "147852369")
	{
	*/
		initBlockUI();
		initHelpAddition();	
		initHelpEdition();
		initHelpPagenameSelection();
		initHelpUpIdRadioBox();
		initHelpAdditionSubmit();
		initHelpMakeXML();
		initHelpModifySelection();
		initHelpModifySubmit();
		initHelpCancelModify();
	/*
	}
	else
	{
		alert("密码错误！");
	}
	*/
});

// 初始化“添加”按钮
function initHelpAddition()
{
	$("#opr_addition").click(function() {
		$("#help_edition").hide();
		$("#help_modify").hide();
		$("#help_addition").show();
		
		if ($("#help_content___Frame").size() == 0) {
			show_FCKEditor("help_content", {height: 300});
		}
		else { set_FCKData("help_content", ""); }
		
		additionBackToZero();
		return false;
	});
}

// 初始化“编辑修改”按钮
function initHelpEdition()
{
	$("#opr_edition").click(function() {
		if ($("#help_addition").css("display") != "block" || confirm("要取消添加新的帮助吗，您刚才所填写的内容都将消失？")) {
			$("#help_addition").hide();
			$("#help_edition").show();
		}
		return false;
	});
}

// 初始化“帮助页面名称”下拉框
function initHelpPagenameSelection()
{
	$("#help_pagename_selection").change(function() {
		if ($(this).val() == "-1") {
			$("#help_pagename").attr("disabled", "disabled");
			$("input:radio[@name='help_upid'][@value='0']").select();
			$("input:radio[@name='help_upid']").attr("disabled", "disabled");
			$("#help_upid_list").hide();
		}
		else if ($(this).val() == "-2") {
			$("#help_pagename").removeAttr("disabled").focus().select();
			$("input:radio[@name='help_upid'][@value='0']").removeAttr("disabled").select();
			$("input:radio[@name='help_upid'][@value='-1']").attr("disabled", "disabled");
			$("#help_upid_list").hide();
		}
		else {
			$("#help_pagename").attr("disabled", "disabled");
			$("input:radio[@name='help_upid']").removeAttr("disabled");
		}
	});
}

// 初始化“上接”单选框
function initHelpUpIdRadioBox()
{
	$("input:radio[@name='help_upid']").click(function() {
		if ($(this).val() == 0) { $("#help_upid_list").hide(); }
		else if ($(this).val() == "-1") {
			var sv = $("#help_pagename_selection").val();
			if (sv != "-1" && sv != "-2")
			{
				var url = "help.php";
				var pars = {
					page_status: 1,
					helpname: sv
				};
				$.post(url, pars, function(xml) {
					$("#help_upid_list").html("");
					$("item", xml).each(function(i) {
						var id = $("id", this).text();
						var title = $("title", this).text();
						$("<div></div>")
							.append($("<input type=\"radio\" />")
									.attr("name", "help_upid_list_radio")
									.attr("hid", id)
									.val(id)
							)
							.append(title)
							.appendTo("#help_upid_list");
					});
					$("#help_upid_list").show();
				});
			}
		}
	});
}

// 初始化“提交”按钮
function initHelpAdditionSubmit()
{
	$("#help_submit_newhelp").click(function() {
		var sv = $("#help_pagename_selection").val();
		var helpname = $("#help_pagename").val();
		var title = $("#help_title").val();
		var helpupid = 0;
		if (sv == "-1") { alert("您还未选择新增帮助所处的页面！"); return false; }
		if (sv == "-2" && $.trim(helpname) == "") { alert("请输入新增帮助所处的页面名称！"); $("#help_pagename").focus().select(); return false; }
		if ($("input:radio[@name='help_upid'][@value='-1']:checked").size() == 1 && $("input:radio[@name='help_upid_list_radio']:checked").size() == 0)
		{ alert("请选择上接的标题！"); return false; }
		if ($("input:radio[@name='help_upid'][@value='-1']:checked").size() != 0) { helpupid = $("input:radio[@name='help_upid_list_radio']:checked").val(); }
		if ($.trim(title) == "") { alert("请输入帮助标题！"); $("#help_title").focus().select(); return false; }
		
		var url = "help.php";
		var pars = {
			page_status:		2,
			helpflag:			sv,
			helpname:		$("#help_pagename").val(),
			helpupid:			helpupid,
			helptitle:			title,
			helpcontent:		get_FCKData("help_content")
		};
		$.post(url, pars, function(text) {
			if (/^[Ss]uccess/.test(text))
			{ alert("添加成功！您可以点击“生成帮助 XML”按钮来生成 XML 页面！"); $("#help_addition").hide(); }
			else
			{ alert("添加失败！请重试！"); }
		});
		return false;
	});
}

// 初始化“生成帮助 XML”按钮
function initHelpMakeXML()
{
	$("#opr_makeXML").click(function() {
		var url = "help.php";
		var pars = {
			page_status: 10
		};
		$.post(url, pars, function(text) {
			if (/^[Ss]uccess/.test(text)) { alert("生成 XML 成功！"); }
			else { alert("生成 XML 失败！"); }
		});
		return false;
	});
}

// 清零“添加”操作
function additionBackToZero()
{
	$("#help_pagename, #help_title").html("");
	$("#help_pagename_selection option[@value='-1']").select();
	$("#help_pagename").attr("disabled", "disabled");
	$("input:radio[@name='help_upid']").eq(0).select();
	$("#help_upid_list").hide();
}

// 初始化修改帮助页面的选择框
function initHelpModifySelection()
{
	$("#help_modify_selection").change(function() {
		var url = "help.php";
		var helpname = $(this).val();
		var pars = {
			page_status: 3,
			helpname: helpname
		};
		$.post(url, pars, function(xml) {
			$("#tbody2").html("");
			$("item", xml).each(function(i) {
				var id			= $("id", this).text();
				var title			= $("title", this).text().unescapeString();
				var addtime		= $("addtime", this).text().split(" ")[0];
				var deleted		= $("deleted", this).text();
				var del_checked	= parseInt(deleted, 10) ? " checked=\"checked\" " : "";
				$("<tr></tr>")
					.append($("<th></th>").html(i + 1))
					.append($("<td></td>").html(title))
					.append($("<td></td>").html(addtime))
					.append($("<td></td>")
							.append($("<input type=\"checkbox\" " + del_checked + "/>")
									.attr("hid", id)
									.click(function() {
										delHelpItem($(this).attr("hid"), this.checked);
									})
							)
							.append(" 删除 ")
							.append($("<a></a>")
									.attr("href", "#")
									.attr("hid", id)
									.html("修改")
									.click(function() {
										modifyHelpItem($(this).attr("hid"));
										return false;
									})
							)
							.append(" ")
							.append($("<a></a>")
									.attr("href", "#")
									.attr("hid", id)
									.html("隶属")
									.click(function() {
										changeHelpUpId(helpname, $(this).attr("hid"), title);
										return false;
									})
							)
							.append(" ")
							.append($("<a></a>")
									.attr("href", "#")
									.attr("hid", id)
									.html("排列")
									.click(function() {
										changeHelpPosition(helpname, $(this).attr("hid"));
										return false;
									})
							)
					)
				.appendTo("#tbody2");
				tablePretty("#tbody2");
			});
		});
	});
}

// 删除帮助条例
function delHelpItem(hid, f)
{
	var deleted = f ? 1 : 0;
	var url = "help.php";
	var pars = {
		page_status: 4,
		hid: hid,
		deleted: deleted
	};
	$.post(url, pars);
}

// 修改帮助项
function modifyHelpItem(hid)
{
	var url = "help.php";
	var pars = {
		page_status: 5,
		hid: hid
	};
	$.post(url, pars, function(xml) {
		var title = $("title", xml).text().unescapeString();
		var content = $("content", xml).text().unescapeString();
		$("#help_modify_id").val(hid);
		$("#help_modify_title").val(title);
		$("#help_edition").hide();
		$("#help_modify").show();
		if ($("#help_modify_content___Config").size() == 0) {
			$("#help_modify_content").val(content);
			show_FCKEditor("help_modify_content", {height: 300});
		}
		else
		{
			set_FCKData("help_modify_content", content);
		}
	});
}

// 初始化帮助修改的提交按钮
function initHelpModifySubmit()
{
	$("#help_modify_submit").click(function() {
		var id = $("#help_modify_id").val();
		var title = $("#help_modify_title").val();
		var content = get_FCKData("help_modify_content");
		if ($.trim(title) == "") { alert("请输入主题！"); $("#help_modify_title").focus().select(); return false; }
		
		var url = "help.php";
		var pars = {
			page_status: 6,
			hid: id,
			title: title,
			content: content
		};
		$.post(url, pars, function(text) {
			if (/^[Ss]uccess/.test(text))
			{
				$("#help_modify").hide();
				$("#help_edition").show();
			}
		});
		
		return false;
	});
}

// 取消修改
function initHelpCancelModify()
{
	$("#help_cancel_modify").click(function() {
		$("#help_modify").hide();
		$("#help_edition").show();
		return false;
	});
}

// 改变帮助隶属
function changeHelpUpId(helpname, hid, helptitle)
{
	var url = "help.php";
	var pars = {
		page_status: 3,
		helpname: helpname
	};
	$.post(url, pars, function(xml) {
		var b = $("<div></div>")
				.append($("<div></div>")
					.append($("<input type=\"radio\" />")
						.attr("name", helpname + "_changeUpId")
						.val(0))
					.append(" ")
					.append($("<span></span>").css({color: "orange", fontSize: "10pt", fontWeight: "bold"}).html("根"))
				);
		$("item", xml).each(function(i) {
			var id = $("id", this).text();
			if (id != hid)
			{
				var title = $("title", this).text().unescapeString();
				b.append($("<div></div>")
					.append($("<input type=\"radio\" />")
						.attr("name", helpname + "_changeUpId")
						.val(id)
					)
					.append(" " + title)
				);
			}
		});
		$.modal($("<div></div>")
			.append($("<div></div>")
					.addClass("modal_header")
					.html("更改位置")
			)
			.append($("<div></div>")
					.addClass("modal_content")
					.append($("<span></span>")
							.css({color: "green", fontWeight: "bold", fontSize: "10pt"})
							.html("-- " + helptitle))
					.append("<h4>请选择本帮助的上级位置：</h4>")
					.append(b)
			)
			.append($("<div></div>")
					.addClass("modal_footer")
					.html($("<a></a>")
							.addClass("buttons")
							.addClass("small_button")
							.attr("href", "#")
							.html("提 交")
							.click(function() {
								if ($("input:radio[@name='" + helpname + "_changeUpId']:checked").size() == 0) {
									alert("请选择一个上级位置！");
									return false;
								}
								var upid = $("input:radio[@name='" + helpname + "_changeUpId']:checked").val();
								var url = "help.php";
								var pars = {
									page_status: 7,
									hid: hid,
									upid: upid
								};
								$.post(url, pars, function(text) {
									if (/^[Ss]uccess/.test(text)) {
										$.modal.close();
									}
									else {
										alert("修改失败，请重试！");
									}
								});
								return false;
							})
					)
			)
		);
	});
}

// 改变帮助位置
function changeHelpPosition(helpname, hid)
{
	var url = "help.php";
	var pars = {
		page_status: 8,
		helpname: helpname,
		hid: hid
	};
	$.post(url, pars, function(xml) {
		var tmpArray = {};
		var b = $("<table></table>").attr("cellspacing", 0).append(
				$("<tr></tr>")
					.append($("<th></th>").html("标题"))
					.append($("<th></th>").html("顺序"))
		);
		$("item", xml).each(function(i) {
			var id = $("id", this).text();
			var title = $("title", this).text().unescapeString();
			var squence = $("squence", this).text();
			tmpArray[id] = { title: title, squence: squence };
		});
		$.each(tmpArray, function(i, n) {
			b.append($("<tr></tr>")
					.append($("<td></td>").html(n["title"]))
					.append($("<td></td>")
							.append($("<input type=\"text\" />")
									.addClass("help_squence_text")
									.attr("hid", i)
									.val(n["squence"])
							)
					)
			);
		});
		$.modal($("<div></div>")
				.append($("<div></div>")
						.addClass("modal_header")
						.html("排列顺序")
				)
				.append($("<div></div>")
						.addClass("modal_content")
						.append($("<div></div>")
								.html("请为下面的帮助文档填写排列顺序（小先大后）：")
								.css({color: "orange", fontSize: "10pt"})
						)
						.append(b)
				)
				.append($("<div></div>")
						.addClass("modal_footer")
						.append($("<a></a>")
								.addClass("buttons")
								.addClass("small_button")
								.attr("href", "#")
								.html("提 交")
								.click(function() {
									var f = true;
									$("input.help_squence_text").each(function(i) {
										if ($.trim($(this).val()) == "") { alert("请填写序列！"); $(this).focus().select(); f = false; return false; }
										if (!/^\d+$/.test($.trim($(this).val()))) { alert("序列必须为自然数！"); $(this).focus().select(); f = false; return false; }
									});
									if (!f) { return false; }
									var squence_obj = {};
									$("input.help_squence_text").each(function(i) {
										var hid = $(this).attr("hid");
										var squence = $(this).val();
										squence_obj[hid] = squence;
									});
									var url = "help.php";
									var pars = {
										page_status: 9,
										squence: JSON.stringify(squence_obj)
									};
									$.post(url, pars, function(text) {
										if (/^[Ss]uccess/.test(text)) {
											$.modal.close();
										}
										else
										{
											alert("更新失败，请重试！");
										}
									});
									return false;
								})
						)
				)
		);
	});
}
